package at.fhv.androideightpuzzle.activities;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import at.fhv.androideightpuzzle.R;
import at.fhv.androideightpuzzle.application.IObserverArguments;
import at.fhv.androideightpuzzle.application.MultiPlayerHostController;
import at.fhv.androideightpuzzle.network.ConnectionType;

public class AcceptClientsActivity extends Activity implements Observer,OnClickListener{
	
	private static final int REQUEST_ENABLE_BLUETOOTH=1;
	
	private MultiPlayerHostController m_controller;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		m_controller=MultiPlayerHostController.getInstance();
		m_controller.addObserver(this);
		
		setContentView(R.layout.accept_clients);
		
		Spinner spinner=(Spinner)findViewById(R.id.spinnerConnectionType);
		ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.connection_types,android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(1);
		
		((Button)findViewById(R.id.btnAcceptClients)).setOnClickListener(this);
		((Button)findViewById(R.id.btnSelectPuzzle)).setOnClickListener(this);
		
		((Button)findViewById(R.id.btnSelectPuzzle)).setEnabled(false);
	}
	
	@Override
	protected void onStart(){
		super.onStart();
	}
	
	@Override
	protected void onActivityResult(int requestCode,int resultCode,Intent data){
		if(requestCode==AcceptClientsActivity.REQUEST_ENABLE_BLUETOOTH){
			if(resultCode==RESULT_OK){
				m_controller.acceptClients(ConnectionType.BLUETOOTH);
			}else if(resultCode==RESULT_CANCELED){
				Toast toast=Toast.makeText(this,"Bluetooth is njot enabled!",Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER,0,0);
				toast.show();
			}
		}
	}
	
	public void onClick(View view){
		if(view.getId()==R.id.btnAcceptClients){
			String itemStr=(String)((Spinner)findViewById(R.id.spinnerConnectionType)).getSelectedItem();
			ConnectionType type=ConnectionType.valueOf(ConnectionType.class,itemStr);
			
			if(type.equals(ConnectionType.BLUETOOTH)){
				BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
				
				if(ba!=null){
					if(!ba.isEnabled()){
						Intent enableBluetoothIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
						startActivityForResult(enableBluetoothIntent,AcceptClientsActivity.REQUEST_ENABLE_BLUETOOTH);
					}
				}else{
					Toast toast=Toast.makeText(this,"Bluetooth is not supported!",Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER,0,0);
					toast.show();
				}
			}else{
				((Button)findViewById(R.id.btnAcceptClients)).setEnabled(false);
				((Button)findViewById(R.id.btnSelectPuzzle)).setEnabled(true);
				
				m_controller.acceptClients(ConnectionType.TCP);
				
				String[] clientNames=m_controller.getClientNames();
				StringBuilder sb=new StringBuilder();
				
				for(int i=0; i<clientNames.length; i++){
					sb.append(clientNames[i]).append("\n");
				}
				
				((TextView)findViewById(R.id.tvAcceptedClients)).setText(sb.toString());
			}
			
			Toast toast=Toast.makeText(this,R.string.accept_clients_toast,Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER,0,0);
			toast.show();
		}else if(view.getId()==R.id.btnSelectPuzzle){
			m_controller.stopAcceptingClients();
			
			Intent intent=new Intent(this,SelectPuzzleActivity.class);
			intent.putExtra(IntentExtraKeys.IS_MULTI_PLAYER_HOST,true);
			intent.putExtra(IntentExtraKeys.IS_MULTI_PLAYER_CLIENT,false);
			
			startActivity(intent);
		}
	}
	
	public final Handler m_handler=new Handler(){
		
		@Override
		public void handleMessage(Message msg){
			if(msg.what==IHandlerMessageKeys.CLIENT_JOINED){
				String[] clientNames=m_controller.getClientNames();
				StringBuilder sb=new StringBuilder();
				
				for(int i=0; i<clientNames.length; i++){
					sb.append(clientNames[i]).append("\n");
				}
				
				((TextView)findViewById(R.id.tvAcceptedClients)).setText(sb.toString());
			}
			
			super.handleMessage(msg);
		}
	};
	
	public void update(Observable observable, Object argument){
		if(observable instanceof MultiPlayerHostController){
			if(argument.equals(IObserverArguments.CLIENT_JOINED)){
				Message msg=m_handler.obtainMessage(IHandlerMessageKeys.CLIENT_JOINED);
				msg.setTarget(m_handler);
				msg.sendToTarget();
			}
		}
	}
}
